package com.xiaoj;

public class ThreadDemo2 {

    public static void main(String[] args) throws InterruptedException {
        //实例化线程类
        ImplementsRunnable runnable = new ImplementsRunnable();
        // 由于是实现 Runnable 接口，接口中就只有一个 run方法
        //所以还是需要使用 Thread 来启动线程
        Thread thread = new Thread(runnable);
        thread.run();
        for (int i = 0; i < 5; i++) {
            System.out.println("main 线程名 = " + Thread.currentThread().getName() + "次数 = " + i);
            Thread.sleep(300);
        }
    }
}

class ImplementsRunnable implements Runnable{
    private Integer count = 0;
    @Override
    public void run() {
        //在 run 方法中编写自己的逻辑
        while (true){

            System.out.println("继承 Thread 方式，创建线程" + (++count) + " 线程名 = " + Thread.currentThread().getName());
            // 进行睡眠 1 秒
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //退出线程
            if(count == 3){
                break;
            }
        }
    }
}
